<?php
namespace StarCloud\Controllers;

use Phalcon\Di;
use Phalcon\Dispatcher;
use Phalcon\Http\Response;

/**
 * @property Response $response
 */
abstract class AbstractController extends \Phalcon\Mvc\Controller
{
    public function renderJson($data, $statusCode = 200)
    {
        $this->response->setStatusCode($statusCode);
        $data = [
            'status' => 0,
            'msg' => 'ok',
            'data' => $data
        ];
        $this->response->setJsonContent($data, JSON_UNESCAPED_SLASHES);
        return $this->response->send();
    }

    public function renderStatusCode($statusCode)
    {
        $this->response->setContentType('application/json', 'UTF-8');
        $this->response->setStatusCode($statusCode);
        return $this->response->send();
    }

    // 事件回收， 如果有手动的触发事件
    public function afterExecuteRoute(Dispatcher $dispatcher)
    {
        Di::getDefault()->get('systemEventsComponent')->executeAllEvents();
    }
}
